part of '../index.dart';

abstract class ImAPI {
  static Future<(List<ImGitfModel>, List<ImGitfWrapperModel>)?> giftList({
    showLoading = false,
  }) async {
    final resp = await HttpHelper.instance().post(
      HttpApi.imGiftList,
      showLoading: showLoading,
      showToast: true,
      body: {'actionType': 7},
    );
    if (!resp.isSuccess) return null;
    if (resp.result is! Map) return null;
    final giftList = resp.result['giftList'];
    final classicRelGift = resp.result['classicRelGift'];
    List<ImGitfModel> gifts = [];
    List<ImGitfWrapperModel> wrappers = [];
    if (giftList is List) {
      gifts = List.generate(
        giftList.length,
        (index) => ImGitfModel.fromJson(giftList[index]),
      );
    }
    if (classicRelGift is List) {
      wrappers = List.generate(
        classicRelGift.length,
        (index) => ImGitfWrapperModel.fromJson(classicRelGift[index]),
      );
    }
    CacheUtil.saveObject(Constants.kGifts, gifts);
    CacheUtil.saveObject(Constants.kGiftWrapper, wrappers);
    return (gifts, wrappers);
  }

  static (List<ImGitfModel>, List<ImGitfWrapperModel>) giftLocalList() {
    List<ImGitfModel> gifts = [];
    List<ImGitfWrapperModel> wrappers = [];
    final giftStr = CacheUtil.get(Constants.kGifts);
    final wrapperStr = CacheUtil.get(Constants.kGiftWrapper);
    if (giftStr.isNotEmpty) {
      gifts = List<ImGitfModel>.from(
        jsonDecode(giftStr).map((x) => ImGitfModel.fromJson(x)),
      );
    }
    if (wrapperStr.isNotEmpty) {
      wrappers = List<ImGitfWrapperModel>.from(
        jsonDecode(wrapperStr).map((x) => ImGitfWrapperModel.fromJson(x)),
      );
    }
    return (gifts, wrappers);
  }

  static Future<RtcTicketModel?> rtcToken(String channelName) async {
    final resp = await HttpHelper.instance().post(
      '/api/v2/im/rtc-token',
      showLoading: false,
      showToast: true,
      body: {'channelName': channelName},
    );
    if (resp.result is! Map) return null;
    return RtcTicketModel.fromJson(resp.result);
  }

  static Future<List<NoticeModel>> notices({
    showLoading = false,
    // NOTICE_ADMIN:系统通知，ORDER_ADMIN:订单消息，PROCLAMATION_ADMIN:官方公告
    required String fromImId,
    int pageNum = 1,
    int pageSize = 10,
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/pushmsg/list?pageSize=$pageSize&pageNum=$pageNum&fromImId=$fromImId',
      showLoading: showLoading,
      showToast: true,
    );
    if (resp.result is! List) return [];
    return List.generate(
      resp.result.length,
      (index) => NoticeModel.fromJson(resp.result[index]),
    );
  }

  static Future<NoticeCountModel?> noticeCount() async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/pushmsg/unreadNum',
      showLoading: false,
      showToast: true,
    );
    if (resp.result is! Map) return null;
    return NoticeCountModel.fromJson(resp.result);
  }

  // NOTICE_ADMIN:系统通知，ORDER_ADMIN:订单消息，PROCLAMATION_ADMIN:官方公告
  static Future<bool> clearNoticeCount(String fromImId) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/pushmsg/msgRead',
      showLoading: false,
      showToast: true,
      body: {'fromImId': fromImId},
    );
    return resp.isSuccess;
  }

  static Future<Set<String>> getImStatus(String imId) async {
    final resp = await HttpHelper.instance().post(
      '/api/v2/user/im/user-online',
      showLoading: false,
      showToast: true,
      body: {'imIds': imId},
    );
    if (resp.result['online'] is! List) return {};
    return List.of(resp.result['online']).map((e) => e.toString()).toSet();
  }

  static Future<MomentEntity?> getMomentEntity(String id) async {
    final resp = await HttpHelper.instance().post(
      '/api/v4/dynamic/find',
      showLoading: true,
      showToast: true,
      body: {'dynamicId': id},
    );
    if (resp.result is! Map) return null;
    return MomentEntity.fromJson(resp.result);
  }

  static Future<int?> getBalance() async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/user/balance/get',
      showLoading: false,
      showToast: true,
    );
    if (resp.result is! Map) return null;
    return int.tryParse(resp.result['virtualBalance'].toString());
  }

  static Future<bool> sendGift({
    required int count,
    required int id,
    required String userId,
  }) async {
    final arr = userId.split('_');
    if (arr.length != 2) return false;
    final uid = arr.first;
    final resp = await HttpHelper.instance().post(
      '/api/v1/virtual-product/gift-im-send',
      showLoading: true,
      showToast: true,
      body: {
        'actionType': '7',
        'amount': count,
        'productId': id,
        'userIds': uid,
        'timestamp': DateTime.now().millisecondsSinceEpoch,
      },
    );

    return resp.isSuccess;
  }
}
